package com.jianqiao.delivery.delivery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jianqiao.delivery.delivery.domain.ExpressPriceEntity;
import com.jianqiao.delivery.delivery.mapper.ExpressPriceMapper;
import com.jianqiao.delivery.delivery.service.ExpressPriceService;
import com.jianqiao.delivery.expresscity.entity.ProvinceEntity;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * author: cyx
 * create: 2024/8/30 9:26
 **/
@Service
public class ExpressPriceServiceImpl extends ServiceImpl<ExpressPriceMapper ,ExpressPriceEntity> implements ExpressPriceService {

    @Autowired
    private ExpressPriceMapper expressPriceMapper;

    /**
     *methood: 根据邮编和街道查询价格
     *auth: cyx
     *time: 2024/8/30 9:47
     **/
    @Override
    public ExpressPriceEntity selectPrice(Map<String ,String> paramMap) {
        String postalCode = paramMap.get("postalCode");
        String province = paramMap.get("province");
        String city = paramMap.get("city");
        String district = paramMap.get("district");
        String street = paramMap.get("street");

        Integer postalCodeInt = null;
        if (!"".equals(postalCode) && postalCode != null){
            postalCodeInt = Integer.valueOf(postalCode);
        }
        LambdaQueryWrapper<ExpressPriceEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isNull(postalCode) && !StringUtils.isNull(street)){
            queryWrapper.eq(ExpressPriceEntity::getPostalCode ,postalCodeInt)
                    .or()
                    .eq(ExpressPriceEntity::getProvince ,province)
                    .eq(ExpressPriceEntity::getCity ,city)
                    .eq(ExpressPriceEntity::getDistrict ,district)
                    .eq(ExpressPriceEntity::getStreet ,street);
        } else {
            if (!StringUtils.isNull(postalCode)){
                queryWrapper.eq(ExpressPriceEntity::getPostalCode ,postalCodeInt);
            } else if (!StringUtils.isNull(street)) {
                queryWrapper.eq(ExpressPriceEntity::getProvince ,province)
                        .eq(ExpressPriceEntity::getCity ,city)
                        .eq(ExpressPriceEntity::getDistrict ,district)
                        .eq(ExpressPriceEntity::getStreet ,street);
            }
        }
        ExpressPriceEntity expressPriceEntity = expressPriceMapper.selectOne(queryWrapper);
        return expressPriceEntity;
    }

    /**
      *methood: 查询雅加达-印尼城市树状表
      *auth: cyx
      *time: 2024/8/30 15:20
    **/
    @Override
    public List<ProvinceEntity> selectExpressTree() {
        List<ProvinceEntity> provinceEntityList = expressPriceMapper.selectProvinceTree();
        return provinceEntityList;
    }
}
